﻿// (c) Copyright Mikhail Dikov. All other rights reserved.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI;
using AjaxControlToolkit;
using System.Xml.Serialization;
using System.ComponentModel;
using WebPartExtenders;
using System.Web.UI.HtmlControls;
// not implemented

namespace WebPartExtenderToolkit
{
    [ToolboxData("<{0}:SplitterWebPart runat=server></{0}:SplitterWebPart>")]
    [XmlRoot(Namespace = "SplitterWebPart")]
    public class SplitterWebPart : ExtenderWebPartBase
    {
        public SplitterWebPart()
        {
            this.TargetType = ExtenderWebPartType.WebPartZone;
        }
        private SplitterWebPartExtender extender;


        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            extender = new SplitterWebPartExtender();
            if (this.Zone != null)
            {
                Control zoneParent = this.Zone.Parent;
                zoneParent.Init += new EventHandler(zoneParent_Init);
            }
        }

        // The actual extender control must be rendered outside of the web part zone in order to work.
        void zoneParent_Init(object sender, EventArgs e)
        {
            Control zoneParent = sender as Control;
            zoneParent.Controls.AddAt(zoneParent.Controls.Count - 1, extender);
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (this.TargetControlID != null)
            {
                extender.TargetControlID = this.TargetControlID;
                extender.Enabled = Enabled && !string.IsNullOrEmpty(TargetControlID);
                extender.BehaviorID = this.BehaviorID;
                //extender.DragHandleID = this.FindControl("Title").ID;
                extender.ID = this.ID + "SplitterExtender";
                //Always hide the extender control chrome, because it'll show up as an empty webpart
                ChromeType = PartChromeType.None;
                Title = "Splitter WebPart Extender";
            }
        }
        protected override void RenderContents(HtmlTextWriter writer)
        {
            base.RenderContents(writer);
            if (this.TargetControlID == null)
            {
                writer.WriteLine("Not configured..");
            }
            else
            {
                //this.RenderContents(writer);
            }
        }

    }
}
